<script setup>
    import { sdCityList } from '@/utils/index'
    import { ElMessage } from 'element-plus'
    import { jobLibraryAdd, jobLibraryEdit } from '@/common/apis'
    import FormList from '@/components/FormList.vue'

    const emit = defineEmits(['addSuccess'])

    let dialogVisible = $ref(false)
    let form = $ref({})

    let showType = $ref('')

    const showAddPost = (data, type) => {
        form = JSON.parse(JSON.stringify(data))
        showType = type
        dialogVisible = true
    }

    const formItem = [
        {
            label: '姓名',
            value: 'name'
        },
        {
            label: '性别',
            value: 'sex',
            type: 'select',
            selectData: ['男', '女']
        },
        {
            label: '身份证号码',
            value: 'idCard',
        },
        {
            label: '民族',
            value: 'nation',
        },
        {
            label: '政治面貌',
            value: 'politaues',
        },
        {
            label: '所属市',
            value: 'city',
            type: 'select',
            selectData: sdCityList
        },
        {
            label: '文化程度',
            value: 'culture',
        },
        {
            label: '出生日期',
            value: 'birth',
            type: 'date',
        },
        {
            label: '人员类型',
            value: 'personGenre',
        }
    ]

    const changeForm = (v, data) => {
        form[v] = data
    }

    const handleClick = () => {
        let api = jobLibraryAdd
        if(form.id) api = jobLibraryEdit
        api(form).then(res => {
            if(res.success){
                ElMessage.success('操作成功~')
                dialogVisible = false
                emit('addSuccess')
            }else{
                ElMessage.error(res.message)
            }
        })
    }

    defineExpose({
        showAddPost
    })

</script>

<template>
    <el-dialog
        v-model="dialogVisible"
        :title="showType == 'edit' ? '修改' : showType == 'add' ? '新增' :'详情'"
        custom-class="zz-dialog"
        width="900px"
        v-if="dialogVisible"
    >	
        <form-list :list="formItem" :formData="form" :type="showType" @changeForm="changeForm"></form-list>

        <template #footer v-if="showType != 'details'">
            <span class="dialog-footer">
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="handleClick">
                    确定
                </el-button>
            </span>
        </template>
    </el-dialog>
</template>
